//
//  QTFamousMapController.m
//  QTLifeCircle
//
//  Created by 王琼 on 17/1/18.
//  Copyright © 2017年 徐腾. All rights reserved.
//

#import "QTFamousMapController.h"
#import <MapKit/MapKit.h>
#import "QTAnnotation.h"

@interface QTFamousMapController ()<MKMapViewDelegate>

@property (strong, nonatomic) IBOutlet MKMapView *mapView;

@property(nonatomic, strong) CLLocationManager *locationManager;

@property(nonatomic, strong) QTAnnotation * annotation;

@end

@implementation QTFamousMapController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self mapViewAddress];
    //标注地图类型
//    _mapView.mapType = MKMapTypeStandard;
    
    //当前视图控制器,赋值给地图视图的代理属性
    self.annotation = [[QTAnnotation alloc] init];
    // 赋值
    self.annotation.coordinate = CLLocationCoordinate2DMake(38, 116);
    // 标题
    self.annotation.title = @"寂寞男孩";
    // 子标题
    self.annotation.subtitle = @"愿阳光不糟,微风刚好,你我都好~";
    [self.mapView addAnnotation:self.annotation];
    
    self.mapView.delegate = self;
    // 创建位置管理者并请求授权
    self.locationManager = [[CLLocationManager alloc] init];
    [self.locationManager requestWhenInUseAuthorization];
    // 设置用户跟踪模式
    self.mapView.userTrackingMode = MKUserTrackingModeFollow;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)mapViewAddress
{
   if(self.addressStr == nil || [self.addressStr length] == 0)
   {
       return;
   }
    //创建地理编码对象
    CLGeocoder * geocoder = [[CLGeocoder alloc] init];
    [geocoder geocodeAddressString:self.addressStr completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
       
        if ([placemarks count ] > 0) {
            //移除目前地图上得所有标注点
            [_mapView removeAnnotations:_mapView.annotations];
            
        }
  
        
        
        for (int i = 0; i< [placemarks count]; i++) {
        
            
        CLPlacemark* placemark = placemarks[i];
        
        //调整地图位置和缩放比例,第一个参数是目标区域的中心点，第二个参数：目标区域南北的跨度，
        //第三个参数：目标区域的东西跨度，单位都是米
        MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(placemark.location.coordinate, 255, 255);
        
        //重新设置地图视图的显示区域
        [_mapView setRegion:viewRegion animated:YES];
        
        //实例化大头针
        self.annotation = [[QTAnnotation alloc]init];
        self.annotation.title = placemark.locality;
        
        //把标注点MapLocation 对象添加到地图视图上，一旦该方法被调用，地图视图委托方法mapView：ViewForAnnotation:就会被回调
        [_mapView addAnnotation:self.annotation];
       
        }
    }];
    
}
#pragma mark mapView Delegate 地图 添加标注时 回调

- (MKAnnotationView *) mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>) annotation {
    // 获得地图标注对象
//    MKPinAnnotationView * annotationView = (MKPinAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:@"PIN_ANNOTATION"];
//    if (annotationView == nil) {
//        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"PIN_ANNOTATION"];
//    }
//    // 设置大头针标注视图为紫色
//    annotationView.pinColor = [UIColor redColor] ;
//    // 标注地图时 是否以动画的效果形式显示在地图上
//    annotationView.animatesDrop = YES ;
//    // 用于标注点上的一些附加信息
//    annotationView.canShowCallout = YES ;
//    
//    return annotationView;
    return  nil;

}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    _mapView.centerCoordinate = userLocation.location.coordinate;
}

- (void)mapViewDidFailLoadingMap:(MKMapView *)theMapView withError:(NSError *)error
{
    NSLog(@"error : %@",[error description]);
}


@end
